#! /usr/bin/env python
#coding=utf-8
#-*- coding: UTF-8 -*-
#coder  poorboy(叼毛)
#url  http://www.caipiao365.vip

 
import time
import datetime 


#返回字符串的Unicode字符串
#参数:
#       _str        要转换的字符串
#返回值:
#        RetStr    转换好后的的字符串
 
def GetUtf8Str(_str):
    RetStr = u""
    try:
        if type(_str).__name__!="unicode": 
            RetStr = unicode(_str, "ascii")
        else:
            RetStr = _str
    except Exception as e: 
        RetStr = _str
    return RetStr

#根据时间差值返回字符串形式的时间
#参数:
#       d        时间差值
#返回值:
#        格式化后的字符串时间

def FormatTimeStr(d):
    chunks = (
        (60 * 60 * 24 * 365, u'年'),
        (60 * 60 * 24 * 30, u'月'),
        (60 * 60 * 24 * 7, u'周'),
        (60 * 60 * 24, u'天'),
        (60 * 60, u'小时'),
        (60, u'分'),
    )
    timeStr = u''
    while d > 59:
        for seconds, unit in chunks:
            count = d // seconds
            if(count > 0):
                timeStr = timeStr + str(count) + unit
                break
        d = d % seconds

    if(d > 0):
        timeStr = timeStr + str(d) + u'秒'
    return timeStr
